﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;

namespace Ivy.Common.Framework.DataAccess
{
    public enum DataProviderTypes
    {
        SqlServer = 0,
        Oracle = 1,
        OleDb = 2
    }
    public class DataAccessProviderUtility
    {
        #region Variables & Constants
        const string _DefaultConnectionStringName = "DefaultConnectionStringName";
        const string _DefaultConfigKeyForDataProvider = "DataProvider";
        const string _DefaultConfigKeyForConnectionString = "ConnectionString";
        const string _SqlServerDataProvider = "System.Data.SqlClient";
        const string _OleDbDataProvider = "System.Data.OledbClient";
        const string _OracleDataProvider = "System.Data.OracleClient";
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DbProviderFactory GetDbProviderFactory()
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _ConnectionStringName = _AppSettingsReader.GetValue(_DefaultConnectionStringName, typeof(string)).ToString();
            ConnectionStringSettings _ConnectionStringSettings = ConfigurationManager.ConnectionStrings[_ConnectionStringName];
            string _DataProvider = _ConnectionStringSettings.ProviderName;
            return DbProviderFactories.GetFactory(_DataProvider);
        }
        public static DbProviderFactory GetDbProviderFactory(string AppSettingsConfigKeyForDataProvider)
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _DataProvider = _AppSettingsReader.GetValue(AppSettingsConfigKeyForDataProvider, typeof(string)).ToString();
            //string _DataProvider = ConfigurationSettings.AppSettings[AppSettingsConfigKeyForDataProvider].ToString();
            return DbProviderFactories.GetFactory(_DataProvider);
        }
        public static DbProviderFactory GetDbProviderFactory(DataProviderTypes DataProviderType)
        {
            string _DataProvider = string.Empty;
            if (DataProviderType == DataProviderTypes.SqlServer)
            {
                _DataProvider = _SqlServerDataProvider;
            }
            if (DataProviderType == DataProviderTypes.Oracle)
            {
                _DataProvider = _OracleDataProvider;
            }
            if (DataProviderType == DataProviderTypes.OleDb)
            {
                _DataProvider = _OleDbDataProvider;
            }
            return DbProviderFactories.GetFactory(_DataProvider);
        }
        public static DbConnection CreateConnection()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateConnection();
        }
        public static DbConnection CreateConnection(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateConnection();
        }
        public static DbConnection CreateConnection(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateConnection();
        }
        public static DbConnection CreateConnectionWithConnectionString()
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _ConnectionStringName = _AppSettingsReader.GetValue(_DefaultConnectionStringName, typeof(string)).ToString();
            ConnectionStringSettings _ConnectionStringSettings = ConfigurationManager.ConnectionStrings[_ConnectionStringName];
            string _ConnectionString = _ConnectionStringSettings.ConnectionString;
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            DbConnection _DbConnection = _DbProviderFactory.CreateConnection();
            _DbConnection.ConnectionString = _ConnectionString;
            return _DbConnection;
        }
        public static DbConnection CreateConnectionWithConnectionString(string AppSettingsConfigKeyForDataProvider, string AppSettingsConfigKeyForConnectionString)
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _ConnectionString = _AppSettingsReader.GetValue(AppSettingsConfigKeyForConnectionString, typeof(string)).ToString();
            //string _ConnectionString = ConfigurationSettings.AppSettings[ConfigKeyForConnectionString].ToString();
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            DbConnection _DbConnection = _DbProviderFactory.CreateConnection();
            _DbConnection.ConnectionString = _ConnectionString;
            return _DbConnection;
        }
        public static DbConnection CreateConnectionWithConnectionString(DataProviderTypes DataProviderType, string ConnectionString)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            DbConnection _DbConnection = _DbProviderFactory.CreateConnection();
            _DbConnection.ConnectionString = ConnectionString;
            return _DbConnection;
        }
        public static DbCommand CreateCommand()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateCommand();
        }
        public static DbCommand CreateCommand(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateCommand();
        }
        public static DbCommand CreateCommand(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateCommand();
        }
        public static DbParameter CreateParameter()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateParameter();
        }
        public static DbParameter CreateParameter(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateParameter();
        }
        public static DbParameter CreateParameter(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateParameter();
        }
        public static DbCommandBuilder CreateCommandBuilder()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateCommandBuilder();
        }
        public static DbCommandBuilder CreateCommandBuilder(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateCommandBuilder();
        }
        public static DbCommandBuilder CreateCommandBuilder(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateCommandBuilder();
        }
        public static DbConnectionStringBuilder CreateConnectionStringBuilder()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateConnectionStringBuilder();
        }
        public static DbConnectionStringBuilder CreateConnectionStringBuilder(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateConnectionStringBuilder();
        }
        public static DbConnectionStringBuilder CreateConnectionStringBuilder(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateConnectionStringBuilder();
        }
        public static DbDataAdapter CreateDataAdapter()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateDataAdapter();
        }
        public static DbDataAdapter CreateDataAdapter(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateDataAdapter();
        }
        public static DbDataAdapter CreateDataAdapter(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateDataAdapter();
        }
        public static DbDataSourceEnumerator CreateDataSourceEnumerator()
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory();
            return _DbProviderFactory.CreateDataSourceEnumerator();
        }
        public static DbDataSourceEnumerator CreateDataSourceEnumerator(string AppSettingsConfigKeyForDataProvider)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(AppSettingsConfigKeyForDataProvider);
            return _DbProviderFactory.CreateDataSourceEnumerator();
        }
        public static DbDataSourceEnumerator CreateDataSourceEnumerator(DataProviderTypes DataProviderType)
        {
            DbProviderFactory _DbProviderFactory = GetDbProviderFactory(DataProviderType);
            return _DbProviderFactory.CreateDataSourceEnumerator();
        }
        public static void AddParameter(DbCommand Command, string ParameterName, ParameterDirection ParamDirection, DbType DataType, object ParameterValue)
        {
            DbParameter _DbParameter = Command.CreateParameter();
            _DbParameter.ParameterName = ParameterName;
            _DbParameter.Direction = ParamDirection;
            _DbParameter.DbType = DataType;
            if (DataType == DbType.String && ParameterValue.ToString() == string.Empty)
            {
                _DbParameter.Value = DBNull.Value;
            }
            else
            {
                _DbParameter.Value = ParameterValue;
            }
            Command.Parameters.Add(_DbParameter);
        }
        public static void AddParameter(DbCommand Command, DbParameter Parameter)
        {
            DbParameter _DbParameter = Command.CreateParameter();
            if (_DbParameter.GetType() == Parameter.GetType())
            {
                Command.Parameters.Add(Parameter);
            }
            else
            {
                throw new Exception("Invalid Parameter! Provider for the Command object and Parameter object are not matching");
            }
        }
        public static void RemoveParameter(DbCommand Command, string ParameterName, DbType DataType, object ParameterValue)
        {
            DbParameter _DbParameter = Command.CreateParameter();
            _DbParameter.ParameterName = ParameterName;
            _DbParameter.DbType = DataType;
            _DbParameter.Value = ParameterValue;
            Command.Parameters.Remove(_DbParameter);
        }
        public static void RemoveParameter(DbCommand Command, DbParameter Parameter)
        {
            DbParameter _DbParameter = Command.CreateParameter();
            if (_DbParameter.GetType() == Parameter.GetType())
            {
                Command.Parameters.Remove(Parameter);
            }
            else
            {
                throw new Exception("Invalid Parameter! Provider for the Command object and Parameter object are not matching");
            }
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
